home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / mgraph / cube3d.c < prev    next >
C/C++ Source or Header  |  1994-03-22  |  3KB  |  121 lines

  1. /* Copyright 1994 Ralph Gonzalez */
  2.  
  3. /*
  4. *    FILE:        cube3d.c
  5. *    AUTHOR:        R. Gonzalez
  6. *    CREATED:    March 16, 1994
  7. *
  8. *    Sample application using mgraph/xgraph and trans routines.
  9. *
  10. *    PROJECT CONTENTS:
  11. *        cube3d.c/.cc, mgraph.c/xgraph.cc, trans.c/.cc
  12. */
  13.  
  14. # ifdef THINK_C
  15. # include    "mgraph.h"
  16. # else
  17. # include    "xgraph.h"
  18. # endif
  19. # include    "trans.h"
  20. # include    <stdio.h>
  21.  
  22. # ifndef PI
  23. # define    PI    3.1415926
  24. # endif
  25.  
  26. void        initialize_cube(Coord3*);
  27. void        transform_cube(Coord3*,double[4][4]);
  28. void        draw_cube(Coord3*,double[4][4]);
  29.  
  30. main()
  31. {
  32.     Coord3    cube[8];
  33.     double    t[4][4],
  34.             ct[4][4],
  35.             wt[4][4];
  36.     Camera    c;
  37.     double    mouse_x,
  38.             mouse_y;
  39.     
  40.     init_graphics();    /* Don't forget to do this FIRST! */
  41.     printf("Mouse position determines velocity, click when done.\n");
  42.     background_color(BLUE);
  43.     erase_graphics();
  44.     
  45.     initialize_cube(cube);
  46.     set_translation(t,-.5,-.5,-.5);    /* center cube on origin */
  47.     transform_cube(cube,t);
  48.     
  49.     /* camera is positioned on the x axis, facing the origin: */
  50.     set_camera(&c,5.,0.,0.,3.*PI/2.,0.,0.);
  51.     set_world_to_camera(wt,c);
  52.     draw_cube(cube,wt);
  53.     
  54.     while (!mouse_button_is_down())
  55.     {
  56.         get_mouse_location(&mouse_x,&mouse_y);
  57.         set_rotation_y(ct,mouse_x*PI/10.);
  58.         set_rotation_z(t,mouse_y*PI/10.);
  59.         combine_trans(ct,t);
  60.         transform_cube(cube,ct); /* apply compound transformation */
  61.         erase_graphics();
  62.         draw_cube(cube,wt);
  63.     }
  64. }
  65.  
  66. /******************************************************************
  67. *    create cube
  68. ******************************************************************/
  69. void    initialize_cube(Coord3 p[])
  70. {
  71.     set_coord(&p[0],0.,0.,0.);
  72.     set_coord(&p[1],1.,0.,0.);
  73.     set_coord(&p[2],1.,1.,0.);
  74.     set_coord(&p[3],0.,1.,0.);
  75.     set_coord(&p[4],0.,0.,1.);
  76.     set_coord(&p[5],1.,0.,1.);
  77.     set_coord(&p[6],1.,1.,1.);
  78.     set_coord(&p[7],0.,1.,1.);
  79. }
  80.  
  81. /******************************************************************
  82. *    transform cube
  83. ******************************************************************/
  84. void    transform_cube(Coord3 p[],double t[4][4])
  85. {
  86.     int        i;
  87.     
  88.     for (i=0 ; i<8 ; i++)
  89.         apply_trans(&p[i],t);
  90. }
  91.  
  92. /******************************************************************
  93. *    project and draw 12 lines of cube. Doesn't draw cube if any
  94. *    point is behind the camera.
  95. ******************************************************************/
  96. void    draw_cube(Coord3 p[],double t[4][4])
  97. {
  98.     int        i;
  99.     Coord2    pict[8];
  100.     boolean    in_front = TRUE;
  101.     
  102.     for (i=0 ; i<8 && in_front ; i++)
  103.         in_front = project(&pict[i],p[i],t,4.);
  104.         
  105.     if (in_front)
  106.     {
  107.         draw_line(pict[0].x,pict[0].y,pict[1].x,pict[1].y);
  108.         draw_line(pict[1].x,pict[1].y,pict[2].x,pict[2].y);
  109.         draw_line(pict[2].x,pict[2].y,pict[3].x,pict[3].y);
  110.         draw_line(pict[3].x,pict[3].y,pict[0].x,pict[0].y);
  111.         draw_line(pict[4].x,pict[4].y,pict[5].x,pict[5].y);
  112.         draw_line(pict[5].x,pict[5].y,pict[6].x,pict[6].y);
  113.         draw_line(pict[6].x,pict[6].y,pict[7].x,pict[7].y);
  114.         draw_line(pict[7].x,pict[7].y,pict[4].x,pict[4].y);
  115.         draw_line(pict[0].x,pict[0].y,pict[4].x,pict[4].y);
  116.         draw_line(pict[1].x,pict[1].y,pict[5].x,pict[5].y);
  117.         draw_line(pict[2].x,pict[2].y,pict[6].x,pict[6].y);
  118.         draw_line(pict[3].x,pict[3].y,pict[7].x,pict[7].y);
  119.     }
  120. }
  121.